package com.hung.triple.heartbeat;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * https://www.sofastack.tech/blog/sofa-rpc-connection-management-heartbeat-analysis/
 *
 * @author Hung
 * @date 2023/2/2 1:42
 */

@Slf4j
public class NettyHeartBeatHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        log.info("client 发送了一个心跳");
        if (evt instanceof IdleStateEvent) {
            RpcHeartbeatTrigger heartbeatTrigger = new RpcHeartbeatTrigger(new RpcCommandFactory());
            heartbeatTrigger.heartbeatTriggered(ctx);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

}
